package com.seal.commons.query;

import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 功能描述: <br/>
 *
 * @author yong.huang
 * @date: 2020-06-24 11:08<br/>
 * @since JDK 1.8
 */
@Slf4j
public class DefaultQueryHandler implements QueryHandler{

    private Map<String, QueryAdapter> adapers;

    @Override
    public <T> QueryResult<T> query(String name, QueryRequest request, Class<T> clazz) {
        if (request == null){
            request = new QueryRequest();
            log.warn("Creating a new queryRequest for the adapter '" + name + "'.");
        }
        QueryAdapter adapter = getAdapter(name);
        QueryResult<T> result = adapter.query(name, request, clazz);
        return result;
    }


    private QueryAdapter getAdapter(String name)
    {
        QueryAdapter adapter = adapers.get(name);
        if (adapter == null){
            throw new NullPointerException("Adapter could not be located: " + name);
        }
        return adapter;
    }

    public void setAdapers(Map map){
        this.adapers = map;
    }




}